﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.User
{
    public partial class editProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorBiography.Width = 450;
                this.Form.DefaultButton = btnUpdateUser.UniqueID;

                BindSubMenu();
                BindData();
                lblConfirmUpdatedUser.Visible = false;

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                GeneralTools.SetCulture(culture);
                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkProfile.NavigateUrl = "~/User/profile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkEditProfile.NavigateUrl = "~/User/editProfile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkChangePassword.NavigateUrl = "~/User/changePassword.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSettings.NavigateUrl = "~/User/editSettings.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSale.NavigateUrl = "~/User/userSale.aspx?uid=" + Request.QueryString["uid"];
            hyLinkPurchases.NavigateUrl = "~/User/userPurchase.aspx?uid=" + Request.QueryString["uid"];
        }

        /// <summary>
        /// Binds the data of a profile, based on the "uid" QueryString.
        /// </summary>
        private void BindData()
        {
            LoginUser user = dbProfile.GetProfileDetails(Guid.Parse(Request.QueryString["uid"]));

            if (user != null)
            {
                txtEmail.Text = user.Email;
                txtFirstName.Text = user.FirstName;
                txtLastName.Text = user.LastName;
                txtPhone.Text = user.Phone;
                ckeditorBiography.Text = user.Biography;
            }
            else
            {
                txtEmail.Enabled = false;
                txtFirstName.Enabled = false;
                txtLastName.Enabled = false;
                txtPhone.Enabled = false;
                ckeditorBiography.Enabled = false;
                btnUpdateUser.Enabled = false;
            }
        }

        /// <summary>
        /// Updates the users profile information, based on the input parameters.
        /// </summary>
        /// <param name="email">Specifies the e-mail to be used in the edit.</param>
        /// <param name="firstName">Specifies the first name to be used in the edit.</param>
        /// <param name="lastName">Specifies the last name to be used in the edit.</param>
        /// <param name="phone">Specifies the phone to be used in the edit.</param>
        /// <param name="bio">Specifies the biography to be used in the edit.</param>
        /// <param name="userId">Specifies the guid of the user to be used in the edit.</param>
        private void UpdateInfo(string email, string firstName, string lastName, string phone, string bio, Guid userId)
        {
            dbProfile.UpdateProfileDetails(email, firstName, lastName, phone, bio, userId);
        }

        protected void btnUpdateUser_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblConfirmUpdatedUser.Visible = true;

                UpdateInfo(txtEmail.Text, txtFirstName.Text, txtLastName.Text, txtPhone.Text, ckeditorBiography.Text, Guid.Parse(Request.QueryString["uid"]));
            }            
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnUpdateUser.Text = GetLocalResourceObject("btnUpdateUserText").ToString();

            hyLinkChangePassword.Text = GetLocalResourceObject("hyLinkChangePasswordText").ToString();
            hyLinkEditProfile.Text = GetLocalResourceObject("hyLinkEditProfileText").ToString();
            hyLinkProfile.Text = GetLocalResourceObject("hyLinkProfileText").ToString();
            hyLinkPurchases.Text = GetLocalResourceObject("hyLinkPurchasesText").ToString();
            hyLinkSale.Text = GetLocalResourceObject("hyLinkSaleText").ToString();
            hyLinkSettings.Text = GetLocalResourceObject("hyLinkSettingsText").ToString();

            lblBiography.Text = GetLocalResourceObject("lblBiographyText").ToString();
            lblConfirmUpdatedUser.Text = GetLocalResourceObject("lblConfirmUpdatedUserText").ToString();
            lblEmail.Text = GetLocalResourceObject("lblEmailText").ToString();
            lblFirstName.Text = GetLocalResourceObject("lblFirstNameText").ToString();
            lblLastName.Text = GetLocalResourceObject("lblLastNameText").ToString();
            lblPhone.Text = GetLocalResourceObject("lblPhoneText").ToString();
            lblsignupFormHeader.Text = GetLocalResourceObject("lblsignupFormHeaderText").ToString();

            string regexEmail = GetLocalResourceObject("regexvalEmailText").ToString();
            regexvalEmail.ToolTip = regexEmail;
            regexvalEmail.ErrorMessage = regexEmail;

            string regexPhone = GetLocalResourceObject("regexvalPhoneText").ToString();
            regexvalPhone.ToolTip = regexPhone;
            regexvalPhone.ErrorMessage = regexPhone;

            string emailValidation = GetLocalResourceObject("rfvalEmailText").ToString();
            rfvalEmail.ToolTip = emailValidation;
            rfvalEmail.ErrorMessage = emailValidation;

            string firstNameValidation = GetLocalResourceObject("rfvalFirstNameText").ToString();
            rfvalFirstName.ToolTip = firstNameValidation;
            rfvalFirstName.ErrorMessage = firstNameValidation;

            string lastNameValidation = GetLocalResourceObject("rfvalLastNameText").ToString();
            rfvalLastName.ToolTip = lastNameValidation;
            rfvalLastName.ErrorMessage = lastNameValidation;

            valsumSignupForm.HeaderText = GetLocalResourceObject("valsumSignupFormText").ToString();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
                ckeditorBiography.Language = "en";
            else
                ckeditorBiography.Language = "da";
        }
    }
}